package top.ffhome.system.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.hutool.core.collection.CollUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import top.ffhome.common.page.PageRequest;
import top.ffhome.common.result.Result;
import top.ffhome.system.entity.SysUser;
import top.ffhome.system.service.SysUserService;
import top.ffhome.util.bean.BeanUtil;

@CrossOrigin
@RestController
@RequestMapping("api/user")
@Api(tags = "系统用户控制器")
public class SysUserController {
	@Autowired
	private SysUserService userService;

	@RequiresPermissions(value = { "system:user:add", "system:user:edit" }, logical = Logical.OR)
	@PostMapping("/save")
	@ApiOperation("新建或保存用户")
	@ApiImplicitParams({ @ApiImplicitParam(name = "password", value = "密码", dataType = "String", paramType = "body"),
			@ApiImplicitParam(name = "name", value = "用户姓名", dataType = "String", paramType = "body"),
			@ApiImplicitParam(name = "status", value = "状态", dataType = "String", paramType = "body"),
			@ApiImplicitParam(name = "roleIds", value = "角色ID列表", paramType = "body", allowMultiple = true, dataType = "Long"),
			@ApiImplicitParam(name = "account", value = "账号,登录时必须", dataType = "String", paramType = "body"),
			@ApiImplicitParam(name = "id", value = "人员ID,修改时必须", dataType = "Long", paramType = "body"),
			@ApiImplicitParam(name = "updateTime", value = "更新时间，乐观锁", dataType = "Date", paramType = "body") })
	public Result save(@RequestBody SysUser user) {
		List<String> fields = CollUtil.newArrayList("password", "name", "status", "roleIds");
		if (user.getId() == null || user.getId() == 0) {
			fields.add("account");
		} else {
			fields.add("id");
			fields.add("updateTime");
		}
		BeanUtil.constraintProperties(user, fields);
		if (user.getId() == null) {
			SysUser u = userService.selectByAccount(user.getAccount());
			if (u != null) {
				return Result.fail("新建用户时，账号重复");
			}
			userService.insert(user);
		} else {
			userService.update(user);
		}
		return Result.success();
	}

	@RequiresPermissions("system:user:remove")
	@PostMapping("/delete")
	@ApiOperation("删除用户")
	@ApiImplicitParams(@ApiImplicitParam(name = "ids", value = "需删除的ID", required = true, paramType = "body", allowMultiple = true, dataType = "Long"))
	public Result delete(@RequestBody List<Long> ids) {
		userService.delete(ids);
		return Result.success();
	}

	@RequiresPermissions("system:user:view")
	@PostMapping("/get")
	@ApiOperation("获取单个用户")
	@ApiImplicitParams(@ApiImplicitParam(name = "id", value = "实例ID", dataType = "Long", required = true, paramType = "body"))
	public Result get(@RequestBody SysUser user) {
		if (user.getId() == null) {
			return Result.fail("参数不正确");
		}
		SysUser u = userService.get(user.getId());
		u.setRoleIds(userService.selectRoleIdsByUserId(user.getId()));
		BeanUtil.deleteProperties(u, CollUtil.newArrayList("password", "salt", "roles", "updateByName"));
		return Result.success(u);
	}

	@RequiresPermissions("system:user:view")
	@PostMapping("/findPage")
	@ApiOperation(value = "用户分页列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "params.nameLike", value = "人员姓名模糊查询", dataType = "String", paramType = "body"),
			@ApiImplicitParam(name = "params.status", value = "状态", dataType = "String", paramType = "body"),
			@ApiImplicitParam(name = "params.role", value = "角色ID", dataType = "Long", paramType = "body") })
	public Result findPage(@RequestBody PageRequest request) {
		return Result.success(userService.select(request));
	}
}
